package com.uidp.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import com.acsior.sbc.exception.BusinessException;
import com.acsior.sbc.exception.SearchException;
import com.acsior.wu.bean.MBGeneralCriteriaSearch;
import com.acsior.wu.bean.utils.MBManager;
import com.acsior.wu.nom.UIListManager;
import com.uidp.domain.Modul;
import com.uidp.search.ModulSearch;
import com.uidp.uitils.ManagedBeans;
import com.uidp.uitils.NavOutcomes;

public class MBSelectModul extends MBGeneralCriteriaSearch<ModulSearch, Modul> {
	private List<String> selected = new ArrayList<String>();
	private String selectedModul;

	public MBSelectModul() {
		super(ModulSearch.class);
		Map<String, Object> values = new HashMap<String, Object>();
		getMBModulHolder().setValues(values);
		try {
			setTableRows(Modul.searchModuls(""));
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Списък с всички Модули
	 * 
	 * @return List
	 */
	public List<SelectItem> getNomModuls() {

		List<Modul> r = getTableRows();
		List<String> modulNames = new ArrayList<String>();
		for (Modul m : r) {
			modulNames.add(m.getModulname());
		}
		if (r != null && !r.isEmpty()) {
			return UIListManager.buildSelectItemsFromStrings(modulNames);
		}

		return new ArrayList<SelectItem>();
	}

	public String navToSelectedModul() {
		try {
			getMBBuildModul().setModul(Modul.searchModuls(getSelectedModul()).get(0));
			return navigate(NavOutcomes.buildModul);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void specificSearch() throws SearchException, BusinessException {
		// TODO Auto-generated method stub

	}

	public List<String> getSelected() {
		return selected;
	}

	public void setSelected(List<String> selected) {
		this.selected = selected;
	}

	public String getSelectedModul() {
		return selectedModul;
	}

	public void setSelectedModul(String selectedModul) {
		this.selectedModul = selectedModul;
	}
	
	/**
	 * @see MBGeneral#getManagedProp()
	 */
	protected ManagedBeans getManagedProp() {
		return ManagedBeans.selectModul;
	}
	/**
	 * Извлича от FacesContext-а MBGroup
	 * 
	 * @return връща MBGroup
	 */
	protected MBBuildModul getMBBuildModul() {
		return (MBBuildModul) MBManager.findBeanInCtx(ManagedBeans.buildModul);
	}
	

	/**
	 * Извлича от FacesContext-а MBGroup
	 * 
	 * @return връща MBGroup
	 */
	protected MBModulHolder getMBModulHolder() {
		return (MBModulHolder) MBManager
				.findBeanInCtx(ManagedBeans.modulHolder);
	}

}
